/**
 * @author helio
 */
if (!SALE.stocks) SALE.stocks = {}

if (!SALE.stocks.ProductsBatchInsertPanel) {
	SALE.stocks.ProductsBatchInsertPanel = Ext.extend(SALE.basicGridPanel, {
	
		layout: 'fit',
		border: false,
		initComponent: function(){
		
			this.initDetailsJson();
			
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'details',
					remoteSort: true,
			        fields: ['code', 'name', 'unit', 'abbr', 'manufacturer', 'category', 'specs', 'model', {name:'price',type:'float'}, {name:'buy_price',type:'float'}]
			    })
			});
			this.detailsStore.load();
					
			// create the grid
			this.detailsGrid = new Ext.grid.EditorGridPanel({
				region:'center',
				store: this.detailsStore,
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
				
				loadMask: true,
				stripeRows: true,
				border: true,
				autoExpandColumn: 'name',
								
				columns: [new Ext.grid.RowNumberer(), {
	           		header: "商品编号",
	           		dataIndex: 'code',
	           		width: 60,
	           		editor: new Ext.form.TextField({
		               allowBlank: false
		           })
	       		},{
	           		header: "商品名称",
	           		dataIndex: 'name',
	           		align: 'left',
	           		editor: new Ext.form.TextField({
		               allowBlank: false
		           })
	        	},{
	           		header: "商品简称",
	           		dataIndex: 'abbr',
	           		width: 60,
	           		align: 'right',
	           		editor: new Ext.form.TextField({
		               allowBlank: true
		           })
	        	}, {
	           		header: "单位",
	           		dataIndex: 'unit',
	           		width: 50,
	           		align: 'right',
	           		editor: new Ext.form.TextField({
		               allowBlank: true
		           })
	        	}, {
	           		header: "生产厂商",
	           		dataIndex: 'manufacturer',
	           		width: 60,
	           		align: 'right',
	           		editor: new Ext.form.ComboBox({
	           			tpl: comboxTpl,
				        store: DataStoreUtil.getManufacturersStore(),
				        displayField:'name',
				        typeAhead: true,
				        mode: 'local',
				        forceSelection: false,
				        triggerAction: 'all'
				    })		
	        	}, {
	           		header: "规格",
	           		dataIndex: 'specs',
	           		width: 50,
	           		align: 'right',
	           		editor: new Ext.form.TextField({
		               allowBlank: true
		           })
	        	}, {
	           		header: "型号",
	           		dataIndex: 'model',
	           		width: 50,
	           		align: 'right',
	           		editor: new Ext.form.TextField({
		               allowBlank: true
		           })
	        	}, {
	           		header: "商品分类",
	           		dataIndex: 'category',
	           		width: 60,
	           		align: 'right',
	           		editor: new Ext.form.ComboBox({
	           			tpl: comboxTpl,
				        store: DataStoreUtil.getCategoriesStore(),
				        displayField:'name',
				        typeAhead: true,
				        mode: 'local',
				        forceSelection: false,
				        triggerAction: 'all',
				        selectOnFocus:true
				    })		
	        	}, {
	           		header: "预设售价",
	           		dataIndex: 'price',
	           		width: 60,
	           		align: 'right',
	           		//renderer: 'usMoney',
	           		editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
	        	}, {
	           		header: "采购价格",
	           		dataIndex: 'buy_price',
	           		width: 60,
	           		align: 'right',
	           		//renderer: 'cnMoney',
	           		editor: new Ext.form.NumberField({
	               		allowBlank: true
	           		})
	        	}],
				
				sm: new Ext.grid.CheckboxSelectionModel(),
				
				tbar:[{
		            text:'插入行',
		            tooltip:'插入行',
		            iconCls:'add',
					handler: this.insertOneRowHandler.createDelegate(this)
		        }, {
		            text:'删除行',
		            tooltip:'删除行',
		            iconCls:'remove',
					handler: this.deleteRowsHandler.createDelegate(this)
		        }, '->', '-', {
		            text:'保存',
		            tooltip:'保存',
		            iconCls:'save',
					handler: this.onSave.createDelegate(this)
		        }, '-']
				
			});
			
			this.items = this.detailsGrid;
			
			SALE.stocks.ProductsBatchInsertPanel.superclass.initComponent.call(this);
		},
		
		initDetailsJson: function() {
			if(!this.detailsJson || !this.detailsJson.details) {
				this.detailsJson = {details: []};
			}
			if(this.detailsJson.details.length < 30) {
				for(var i = this.detailsJson.details.length; i < 30; i++) {
					this.detailsJson.details.push({});
				}
			}
		},
				
		onSave : function() {
			var store = this.detailsGrid.store;
			
			var details = [];
			var index = 0;
			store.each(function(record){
				if(record.data.code && record.data.code != "") {
					details[index] = record.data;
					index ++;
				}		
			});
			if(index) {
				SALE.Action.saveAllProductsInfo(this.saveSuccessHandler.createDelegate(this), details);
			}
		}
	});
	
	Ext.reg('productsbatchinsert', SALE.stocks.ProductsBatchInsertPanel);
}